<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="login-nav-bar" fixed>
      <van-button 
      class="search-btn" 
      slot="title" 
      type="info" 
      size="small" 
      round 
      icon="search"
      to="/search"
      >搜索</van-button>
    </van-nav-bar>

    <!-- tabs -->
    <van-tabs class="tabs" v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 结论：频道内容的组件 -->
        <article-list :channel="item" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburgerBtn" @click="isChannelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道弹出框 -->
    <van-popup closeable close-icon-position="top-left" v-model="isChannelEditShow" position="bottom" :style="{ height: '100%' }">
      <channel-edit :my-channels="channels" :active.sync="active" @update-active="onUpdateActive" />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'

export default {
  name: 'homeIndex',
  components: {
    'article-list': ArticleList,
    'channel-edit': ChannelEdit
  },
  data() {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  methods: {
    async loadUserChannels() {
      try {
        // const { data: res } = await getUserChannels();
        // this.channels = res.data.channels;
        //    console.log(res);
        let channels = []
        // 用户登录
        if (this.user) {
          const { data: res } = await getUserChannels()
          channels = res.data.channels
        } else {
          // 用户未登录
          let localChannel = getItem('TOUTIAO-CHANNELS')
          if (!localChannel) {
            const { data: res } = await getUserChannels()
            channels = res.data.channels
          } else {
            channels = localChannel
          }
        }

        this.channels = channels
      } catch (err) {
        this.$toast.fail('获取用户频道失败')
      }
    },
    onUpdateActive(index, flag) {
      this.active = index

      this.isChannelEditShow = flag
    }
  },
  created() {
    this.loadUserChannels()
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.home-container {
  //   vue脚手架 css 样式穿透：  在父组件给子组件设置样式  >>>   /deep/  ::v-deep
  padding-top: 174px;
  padding-bottom: 100px;
  ::v-deep .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  ::v-deep .tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      left: 0;
      top: 92px;
      z-index: 3;
    }
    .van-tab {
      width: 190px;
      border-right: 1px solid #edeff3;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburgerBtn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
